package core.dispatch.event.player;

import BaseCommon.CommLog;
import business.global.room.RoomMgr;
import business.player.Player;
import cenum.DispatcherComponentEnum;
import cenum.redis.RedisBydrKeyEnum;
import com.ddm.server.common.utils.CommTime;
import com.ddm.server.common.utils.IDCardUtil;
import com.ddm.server.dispatcher.executor.BaseExecutor;
import core.dispatch.DispatcherComponent;
import core.ioc.ContainerMgr;
import core.logger.flow.FlowLogger;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 登出
 *
 * @author Administrator
 */
@Data
public class LogoutEvent implements BaseExecutor {
    private Player player;

    public LogoutEvent(long playerId, Player player) {
        this.player = player;
    }

    @Override
    public void invoke() {
        if (null == getPlayer()) {
            CommLog.error("[LogoutListener]([LogoutEvent]) null == logoutEvent || null == logoutEvent.getPlayer() ");
            return;
        }
        Player player = this.getPlayer();
        // 更新掉线状态
        RoomMgr.getInstance().lostConnect(player, true);
        int lastLogout = CommTime.nowSecond();
        if (CommTime.minTimeDifference(player.getPlayerBO().getLastLogin(), lastLogout) >= 1) {
            FlowLogger.playerLoginLog(player.getPid(), player.getPlayerBO().getLastLogin(), lastLogout, player.getGameList(), player.getIp(),
                    String.valueOf(player.getLocationInfo().getLatitude()), String.valueOf(player.getLocationInfo().getLongitude()),
                    player.getLocationInfo().getAddress());



            // 是否有填写身份证信息
            Integer age = IDCardUtil.getAge(player.getPlayerBO().getRealNumber());
            if (Objects.nonNull(age) && age.intValue() < 18) {
                int secondTimeInt = CommTime.secondTimeEveryDayDifference(player.getPlayerBO().getLastLogin(),lastLogout);
                if (secondTimeInt >= 0) {
                    if (player.isExistTodayFirstLogin()) {
                        // 第一个登录重置时间
                        ContainerMgr.get().getRedis().put(RedisBydrKeyEnum.PLAYER_IDCARD18_LOGIN_TIME.getKey(player.getPid()), String.valueOf(Math.abs(secondTimeInt)));
                    } else if (secondTimeInt >= 5) {
                        // 时间追加
                        ContainerMgr.get().getRedis().incrBy(RedisBydrKeyEnum.PLAYER_IDCARD18_LOGIN_TIME.getKey(player.getPid()), secondTimeInt);
                    } else {
                        CommLog.info("LogoutEvent 1 pid:{},age:{},secondTimeInt:{}",player.getPid(),age,secondTimeInt);
                    }
                } else if (secondTimeInt <= -1) {
                    ContainerMgr.get().getRedis().put(RedisBydrKeyEnum.PLAYER_IDCARD18_LOGIN_TIME.getKey(player.getPid()), String.valueOf(Math.abs(secondTimeInt)));
                }
            }

            int size = ContainerMgr.get().getRedis().getSet(RedisBydrKeyEnum.PID_2_CLUB_SET.getKey(player.getPid())).size();
            if (size <= 0) {
                DispatcherComponent.getInstance().publish(new PlayerIDCardEvent(player.getPid(), player.getPlayerBO().getLastLogin(), lastLogout));
            } else if (age.intValue() >= 18) {
                DispatcherComponent.getInstance().publish(new PlayerIDCardEvent(player.getPid(), player.getPlayerBO().getLastLogin(), lastLogout));
            }
        }

    }

    @Override
    public int threadId() {
        return DispatcherComponentEnum.PLAYER.id();
    }

    @Override
    public int bufferSize() {
        return DispatcherComponentEnum.PLAYER.bufferSize();
    }
}